package bg

import (
	"fmt"
	"server/src/game/we_chat_group"
	"server/src/global"
	"server/src/server"
	"time"
)

func InitBgGame() {
	for {
		currentTime := time.Now()
		fmt.Println("this is big for")
		if currentTime.Hour() >= 7 && currentTime.Hour() < 24 {
			previousAnswerId := server.ServerGuess.GetAnwserId()
			//期数准备
			global.BG_INITIAL_ANSWERID = previousAnswerId
			global.BG_CURRENT_ANSWERID = previousAnswerId + 1
			global.BG_NEXT_ANSWERID = previousAnswerId + 2
			//设置番摊
			global.BG_FT_NUMBERS_STARANSWER = global.BG_CURRENT_ANSWERID
			global.BG_FT_NUMBERS_ENDANSWER = global.BG_CURRENT_ANSWERID
			global.BG_FT_NUMBERS = make([]int, 0)

			ticker := time.Tick(1 * time.Second) // 每1执行一次
			//isOpen := true
			//开奖时间设置
			global.BG_GAME_RUN_WAIT_TIME = global.BG_GAME_WAIT_TIME
			global.BG_GAME_RUN_OPEN_TIME = global.BG_GAME_OPEN_TIME
			//游戏开始指令
			we_chat_group.StartGame(global.BG_CURRENT_ANSWERID)
			//开始循环
			for range ticker {
				//fmt.Println("Time is:", global.BG_GAME_RUN_OPEN_TIME, global.BG_GAME_RUN_WAIT_TIME)

				// Get the current time
				currentTime = time.Now()
				// Check if it's past midnight (i.e., the next day)
				if currentTime.Hour() < 7 && currentTime.Hour() >= 0 {
					fmt.Println("InitBgGame ended at midnight.")
					break
				}
				if global.BG_GAME_RUN_WAIT_TIME == 30 {
					we_chat_group.StopBettingReqPre()
				}
				if global.BG_GAME_RUN_WAIT_TIME <= 0 {
					//if isOpen {
					//	go bgGameOpenAnswer()
					//	isOpen = false
					//}
					//发送停止答题的消息
					if global.BG_GAME_RUN_OPEN_TIME == global.BG_GAME_OPEN_TIME-1 {
						go we_chat_group.StopBettingReq(global.BG_CURRENT_ANSWERID)
					}
					if global.BG_GAME_RUN_OPEN_TIME <= 0 {
						go bgGameOpenAnswer()
						//isOpen = true
						//时间重置
						global.BG_GAME_RUN_WAIT_TIME = global.BG_GAME_WAIT_TIME
						global.BG_GAME_RUN_OPEN_TIME = global.BG_GAME_OPEN_TIME
					} else {
						global.BG_GAME_RUN_OPEN_TIME--
					}
				} else {
					global.BG_GAME_RUN_WAIT_TIME--
				}
			}
		} else {
			// Calculate the time until 7:00 AM of the next day
			day := currentTime

			if currentTime.Hour() > 7 {
				day = currentTime.AddDate(0, 0, 1)
			}

			nextDayMorning := time.Date(day.Year(), day.Month(), day.Day(), 7, 0, 0, 0, day.Location())
			durationUntilNextDay := nextDayMorning.Sub(currentTime)
			go func() {
				for {
					nowTime := time.Now()
					if nowTime.Hour() == 4 && (nowTime.Minute() == 10 || nowTime.Minute() == 40) && nowTime.Second() == 0 {
						//清空流水和抽水，还有上下分
						server.ServerGroupUser.ClearUserTodayWater()
						server.ServerRobotUser.ClearRobotUserTodayWater()
					}
				}
			}()
			// Sleep until 7:00 AM
			time.Sleep(durationUntilNextDay)
			// Run the loop until midnight
		}
	}
}

func bgGameOpenAnswer() {
	//获取全部积分总分
	sumIntegral, err := server.ServerGuess.GetBgCurrentAllIntegral(global.BG_CURRENT_ANSWERID)
	if err != nil {
		panic(any("获取总分出错" + err.Error()))
	}
	//得到本期答案
	numbersStr, superNum, answerNum := getAnswer(global.BG_CURRENT_ANSWERID, sumIntegral)
	//设置答案到服务器
	if err := server.ServerGuess.SetCurrentAnswerNum(numbersStr, superNum, answerNum); err != nil {
		panic(any("设置答案到服务器出错," + err.Error()))
	}
	//开始判定用户输赢
	if err := server.ServerGuess.BgDetermineBetting(answerNum); err != nil {
		panic(any("判定用户输赢出错," + err.Error()))
	}

	//期数重置
	if len(global.BG_FT_NUMBERS) == 91 {
		global.BG_FT_NUMBERS = make([]int, 0)
		global.BG_FT_NUMBERS_STARANSWER = global.BG_CURRENT_ANSWERID
	}
	if len(global.BG_FT_NUMBERS) == 0 {
		global.BG_FT_NUMBERS_STARANSWER = global.BG_CURRENT_ANSWERID
	}
	global.BG_FT_NUMBERS = append(global.BG_FT_NUMBERS, answerNum)
	global.BG_FT_NUMBERS_ENDANSWER = global.BG_CURRENT_ANSWERID

	global.BG_CURRENT_INTERGRAL = 0
	global.BG_INITIAL_ANSWERID = global.BG_CURRENT_ANSWERID
	global.BG_CURRENT_ANSWERID += 1
	global.BG_NEXT_ANSWERID = global.BG_CURRENT_ANSWERID + 1

	go func() {
		//游戏发送玩家结果
		we_chat_group.GameEndResult(global.BG_INITIAL_ANSWERID, superNum, answerNum)
		//发送用户积分
		we_chat_group.SendUserScore()
		//发送图片
		we_chat_group.VeryAnserIdImgReq(global.BG_CURRENT_ANSWERID)
		//发送开始游戏指令
		if (time.Now().Hour() == 23 || time.Now().Hour() < 7) && time.Now().Minute() >= 55 {
			we_chat_group.EndDayGame()
		} else {
			we_chat_group.StartGame(global.BG_CURRENT_ANSWERID)
		}
	}()
}
